import arcpy

##JOHNTALBOT
##THIS SCRIPT DETERMINES THE CLOSEST POINT FOR EACH STORM
##SINGLE FUNCTION SCRIPT
def main():
    #Define table contiainting list of storm points.
    fc= "C:\\storms.gdb\\spatial_point_join"
    cursor=arcpy.UpdateCursor(fc)
    stormlist=[]
    #Loop through storms to determine a list of unique storm IDs with no duplicates
    for i in cursor:
        x=1
        for j in stormlist:
            if i.Serial_Num==j:
                x=0
                break
        if x==1:
            stormlist.append(i.Serial_Num)
    del cursor
    del i

    #For each storm ID, loop cursor to determine point of least distance to shoreline.
    #
    numlist=[]
    for stormID in stormlist:
        minimum=99999999
        cursor=arcpy.UpdateCursor(fc)
        for row in cursor:
            if str(row.Serial_Num) == str(stormID): # &(row.YMD[0]!='A'))
                if float(row.Kilometers) < float(minimum):
                    minimum=float(row.Kilometers)
                    obj=row.OBJECTID
        cursor.reset()
        print stormID, "dist", minimum, obj
        numlist.append(obj)
    print numlist
    del cursor
    #For each point found to be the nearest, flag the record in a new "X" field.
    arcpy.AddField_management(fc,"X","SHORT")
    po=0
    cursor=arcpy.UpdateCursor(fc)
    for p in cursor:
        
        for d in numlist:
            if str(p.OBJECTID) == str(d):
                p.X = 1
                po=po+1
                
        cursor.updateRow(p)
    
    print po
            
        
main()    
    
